package com.ibeeking.found.upms.b.rest.controller.v1.feign;

import com.ibeeking.found.common.constants.HeaderNounConstant;
import com.ibeeking.found.upms.b.rest.service.ITenantUserService;
import com.ibeeking.found.common.db.annotation.IgnoreDS;
import com.ibeeking.found.common.entity.LoginUserDTO;
import com.ibeeking.found.common.entity.UserInfoDTO;
import com.ibeeking.found.upms.api.common.dto.TenantUserDTO;
import com.ibeeking.found.upms.api.common.query.AccountLoginQuery;
import com.ibeeking.found.upms.service.common.vo.TenantListVO;
import com.ibeeking.nematos.utils.data.Collections3;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName BunkInfoController控制器
 * @Description OAuth2.0客户端
 * @Author ibeeking
 * @Date 2020-10-22 10:34
 **/
@RestController
@RequestMapping("/b/user/feign/v1")
public class UserFeignController {

    @Resource
    private ITenantUserService tenantUserServiceImpl;

    /**
     * 根据登录账号查询登录用户信息
     *
     * @param query
     * @return
     */
    @IgnoreDS
    @PostMapping(value = "/queryAccountLogin")
    public LoginUserDTO queryAccountLogin(@RequestBody AccountLoginQuery query) {
        return tenantUserServiceImpl.queryAccountLogin(query);
    }

    /**
     * 根据userId查询全局用户信息
     *
     * @param userId
     * @return
     */
    @PostMapping(value = "/queryLoginUser/{userId}")
    public UserInfoDTO queryLoginUser(@PathVariable("userId") Long userId, HttpServletRequest request) {
        Integer tenantId = Integer.valueOf(request.getHeader(HeaderNounConstant.TENANT_ID));
        return tenantUserServiceImpl.queryLoginUser(userId ,tenantId);
    }

    /**
     * 根据手机号查询租户用户全局用户信息
     *
     * @param mobile
     * @return
     */
    @IgnoreDS
    @PostMapping(value = "/queryByMobile/{mobile}")
    public TenantUserDTO queryByMobile(@PathVariable("mobile") String mobile) {
        return tenantUserServiceImpl.queryByMobile(mobile);
    }


    /**
     * 根据租户ID查询租户用户全局用户信息
     *
     * @param tenantId
     * @return
     */
    @IgnoreDS
    @PostMapping(value = "/queryByTenantId/{tenantId}")
    public List<TenantUserDTO> queryByTenantId(@PathVariable("tenantId") Integer tenantId) {
        return tenantUserServiceImpl.queryByTenantId(tenantId);
    }

    /**
     * 根据用户id查询租户
     *
     * @param userId
     * @return
     */
    @IgnoreDS
    @PostMapping(value = "/queryByUserId/{userId}")
    public List<Integer> queryByUserId(@PathVariable("userId") Long userId) {
        List<TenantListVO> tenantListVos = tenantUserServiceImpl.queryTenants(userId);
        if (Collections3.isEmpty(tenantListVos)) {
            return Collections3.emptyList();
        }
        return tenantListVos.stream().map(TenantListVO::getId).collect(Collectors.toList());
    }
}
